import java.util.Random;
import java.util.function.Consumer;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/2/17 15:57
 * @description:
 * @version:
 */
public class SupplierTest {
    public static void main(String[] args) {
        Supplier<Student> supplier = () -> new Student("kk",12);
        Consumer<Student> consumer = new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println("消费者模式");
            }
        };
        Object o = test2(supplier, consumer);


        Supplier<Integer> supplier1;
        supplier1 = () -> new Integer(1);
        System.out.println(supplier1.get());
    }

    public static void testSupplier() {
        //① 使用Supplier接口实现方法,只有一个get方法，无参数，返回一个值

        Supplier<Integer> supplier = new Supplier<Integer>() {
            @Override
            public Integer get() {
                //返回一个随机值
                return new Random().nextInt();
            }
        };

        System.out.println(supplier.get());

        //② 使用lambda表达式，
        supplier = ()-> new Integer(10);
        System.out.println(supplier.get());

        //③ 使用方法引用
        Supplier<Double> supplier2 = Math::random;
        System.out.println(supplier2.get());

        //4. IntSupplier DoubleSupplier LongSupplier 里面只有一个getAsXXX的方法，
        IntSupplier intSupplier = new IntSupplier() {
            @Override
            public int getAsInt() {
                return new Random().nextInt();
            }
        };

        System.out.println(intSupplier.getAsInt());
    }

    public static class Student {
        private String name;
        private Integer age;
        private Supplier<Integer> supplier;


        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Student(String name, Integer age, Supplier<Integer> supplier) {
            this.name = name;
            this.age = age;
            this.supplier = supplier;
        }

        public Supplier<Integer> getSupplier() {
            return supplier;
        }

        public void setSupplier(Supplier<Integer> supplier) {
            this.supplier = supplier;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Student(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

    }

    public static <R> R test2 (Supplier<Student> supplier, Consumer<Student> consumer) {
        Student student = supplier.get();
        consumer.accept(student);
        return (R) student;
    }

}
